﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using System.Data.SQLite;

namespace Classes
{
   public class Staff
   {
      #region Private Member Variables
      
      static private List<Employee> people = null;
      
      #endregion
      
      #region Accessor Functions
      
      static public List<Employee> Employees { get { if (people == null) Setup(); return people; } }
      
      static public Employee GetEmployee(String type, String param)
      {
         if (people == null)
            Setup();
      
         Employee x = null;
         
         switch (type)
         {
            case "first":
               x = people.Find(
               delegate (Employee current)
               {
                  return param == current.First;
               });
               break;
            case "last":
               x = people.Find(
               delegate (Employee current)
               {
                  return param == current.Last;
               });
               break;
            case "phone":
               x = people.Find(
               delegate (Employee current)
               {
                  return param == current.Phone;
               });
               break;
         }
         
         return x;
      }
      
      #endregion
      
      #region Setup
      
      static public void Setup()
      {
         people = new List<Employee>();

         SQLiteConnection conn = new SQLiteConnection(@"Data Source=C:\Users\toadzky\Documents\Visual Studio 2010\Projects\APX_2010\2010.apx;");
         SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Employees;", conn);
         conn.Open();

         SQLiteDataReader rows = cmd.ExecuteReader();

         while (rows.Read())
            people.Add(new Employee(rows));
            
         people.Sort();
      }
            
      #endregion
   }
   
   public class Employee : IComparable<Employee>
   {
      #region Private Member Variables
      
      private String first, last, phone;
      
      #endregion
      
      #region Properties
      
      public String First { get { return first; } }
      public String Last { get { return last; } }
      public String FirstLast { get { return first + " " + last; } }
      public String LastFirst { get { return last + ", " + first; } }
      public String Phone { get { return String.Format("({0}) {1} - {2}", phone.Substring(0,3), phone.Substring(3,3), phone.Substring(6)); } }
      
      #endregion
      
      #region Constructors
      
      public Employee (SQLiteDataReader iEmp)
      {
         first = (String)iEmp["first"];
         last = (String)iEmp["last"];
         phone = (String)iEmp["phone"];
      }
      
      #endregion
      
      #region Compare Functions
      
      public int CompareTo(Employee b)
      {
         return last.CompareTo(b.Last);
      }
      
      #endregion
   }
}
